package com.hsd.znsh.converter;

import com.hsd.znsh.entity.Product;
import com.hsd.znsh.pojo.output.ProductVO;
import com.hsd.znsh.pojo.output.SensorVO;
import com.hsd.znsh.repository.ProductTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.utils.BeanUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 13:54:52
 */
public class ProductConverter {

    public static ProductVO converterToProductVO(Product product, ProductTypeRepository productTypeRepository,
                                                 UserRepository userRepository){
        ProductVO productVO=new ProductVO();
        BeanUtil.copyProperties(product,productVO);
        if(product.getProductTypeId()!=null){
            productVO.setProductType(productTypeRepository.findOne(product.getProductTypeId()).getName());
        }
        if(product.getCreateUserId()!=null){
            productVO.setCreateUsername(userRepository.findOne(product.getCreateUserId()).getUsername());
        }
        return productVO;
    }

    public static List<ProductVO> converterToProductVOList(List<Product> products, ProductTypeRepository productTypeRepository,
                                                       UserRepository userRepository){
        List<ProductVO> list=new ArrayList<>();
        for(Product product:products){
            list.add(converterToProductVO(product,productTypeRepository,userRepository));
        }
        return list;
    }
}
